import re
import shutil

from curl_cffi import requests
import os

root = 'static/彼岸图库'
if os.path.exists(root):
    shutil.rmtree(root)
os.mkdir(root)


url = 'https://pic.netbian.com/'

header = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'}
header2 = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'referer':'https://pic.netbian.com/'
}

request_response = requests.get(url, headers=header).content.decode('gbk')

items = re.findall(r'<li><a href="(/tupian/\d{5}.html)" title="(.*?)" target="_blank"><span><img src="(.*?)" alt=".*?" /></span>.*?</li>', request_response, re.S)
print(len(items))
for item in items:
    print(item)
    detail_url = f"https://pic.netbian.com{item[0]}"
    title = item[1]
    img_url = f"https://pic.netbian.com{item[2]}"
    print(img_url)

    img_response = requests.get(img_url, headers=header2).content
    with open(f'{root}/{title}.jpg', 'wb') as f:
        f.write(img_response)
